import away3d.cameras.*;

import com.powerfl.frm.taf.*;

final public function get eachs():TAFeach{
	var temp:*;
	temp = tdata( '@eachs' )[flowContext];
	if( temp ){
		return temp;
	}else{
		error( '선언되지 않은 루프참조입니다. ' + flowContext );
	}
	return null;
}

private function get treeVar():Object{
	return tdata( '@treevar' );
}
final public function treeVarGet( $tree:String, $key:String ):*{
	var temp:*;
	temp = treeVar; 
	if( temp[$tree] ){}else{
		error( '선언되지 않은 트리변수 입니다. - tree:' + $tree );
	}
	temp = temp[$tree][$key]; 
	if( temp === undefined ){
		error( '선언되지 않은 트리변수명 입니다. - tree:' + $tree + ', key:' +$key );
	}else{
		return temp;
	}
}
final public function treeVarSet( $tree:String, ...$keyValue ):void{
	var i:int, j:int, temp:*;
	temp = treeVar;
	if( temp[$tree] ){}else{
		temp[$tree] = {};
	}
	temp = temp[$tree];
	for( i = 0, j = $keyValue.length ; i < j ; i += 2 ){
		temp[$keyValue[i]] = $keyValue[i+1]; 
	}
}
private function get global():Object{
	return tdata( '@global' );
}
final public function globalGet( $key:String ):*{
	var temp:*;
	temp = global; 
	if( temp[$key] === undefined ){
		error( '선언되지 않은 전역변수 입니다. - ' + $key );
	}else{
		return temp[$key];
	}
}
final public function globalUp( ...$keys ):void{
	var i:int, j:int, temp:*;
	temp = global;
	for( i = 0, j = $keys.length ; i < j ; ++i ){
		temp[$keys[i]] += 1;
	}
}
final public function globalDown( ...$keys ):void{
	var i:int, j:int, temp:*;
	temp = global;
	for( i = 0, j = $keys.length ; i < j ; ++i ){
		--temp[$keys[i]];
	}
}
final public function globalSet( ...$keyValue ):void{
	var i:int, j:int, temp:*;
	temp = global;
	for( i = 0, j = $keyValue.length ; i < j ; i += 2 ){
		if( temp[$keyValue[i]] === undefined ){
			error( '선언되지 않은 전역변수 입니다. - ' + $keyValue[i] )
		}else{
			temp[$keyValue[i]] = $keyValue[i+1]; 
		}
	}
}
private function get param():Array{
	if( flowContext ){
		return tdata( '@param' )[flowContext] || null;
	}else{
		return null;
	}
}
private function paramSet( $val:Array ):void{
	tdata( '@param' )[flowContext] = $val;
}
private function get local():Object{
	if( flowContext ){
		return tdata( '@' + flowContext ) || null;
	}else{
		return null;
	}
}
final public function localGet( $key:String ):*{
	var temp:*;
	temp = tdata( '@' + flowContext ); 
	if( temp[$key] === undefined ){
		error( '선언되지 않은 지역변수 입니다. - ' + $key );
	}else{
		return temp[$key];
	}
}
final public function localUp( ...$keys ):void{
	var i:int, j:int, temp:*;
	temp = tdata( '@' + flowContext );
	for( i = 0, j = $keys.length ; i < j ; ++i ){
		temp[$keys[i]] += 1;
	}
}
final public function localDown( ...$keys ):void{
	var i:int, j:int, temp:*;
	temp = tdata( '@' + flowContext );
	for( i = 0, j = $keys.length ; i < j ; ++i ){
		--temp[$keys[i]];
	}
}
final public function localSet( ...$keyValue ):void{
	var i:int, j:int, temp:*;
	temp = tdata( '@' + flowContext );
	for( i = 0, j = $keyValue.length ; i < j ; i += 2 ){
		if( temp[$keyValue[i]] === undefined ){
			error( '선언되지 않은 지역변수 입니다. - ' + $keyValue[i] )
		}else{
			temp[$keyValue[i]] = $keyValue[i+1]; 
		}
	}
}
final public function get localScript():Array{
	if( flowContext ){
		return tdata( '@@' + flowContext + '@script' ) || null;
	}else{
		return null;
	}
}
final public function tdata( $key:String ):*{
	var temp:*;
	temp = SELF._trees[$key];
	if( temp ){
		return temp.data;
	}else{
		return null;
	}
}
final public function tdataToGlobal( $key:String, $name:String, $return:String, ...$param ):void{
	var param:TAFparam, temp:*;
	temp = treeGet( $key );
	if( temp ){
		if( $name ){
			if( temp[$name] is Function ){
				global[$return] = temp[$name].apply( null, $param ); 
			}else{
				global[$return] = temp[$name]; 
			}
		}else{
			global[$return] = temp;
		}
	}
}
final public function tdataToLocal( $key:String, $name:*, $return:String, ...$param ):void{
	var param:TAFparam, temp:*, i:int, j:int;
	temp = treeGet( $key );
	if( temp ){
		if( $name is Array ){
			$name = $name.concat();
			for( i = 0, j = $name.length ; i < j ; ++i ){
				$name[i] = temp[$name[i]];
			}
			local[$return] = $name;
		}else{
			if( $name ){
				if( temp[$name] is Function ){
					local[$return] = temp[$name].apply( null, $param ); 
				}else{
					local[$return] = temp[$name]; 
				}
			}else{
				local[$return] = temp;
			}
		}
	}
}
final public function tdataSetArray( $keys:Array, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAdd( param, function():void{
		var temp:*, key:*;
		for( key in $keys ){
			temp = tdata( key );
			if( temp ){
				param.apply( temp );
			}
		}
	} );
}
final public function tdataSet( $key:String, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAdd( param, function():void{
		var temp:*;
		temp = tdata( $key );
		if( temp ){
			param.apply( temp );
		}
	} );
}
final public function tdata_Set( $key:String, ...$param ):void{
	var param:TAFparam, temp:*;
	param = $paramGet( $param );
	temp = tdata( $key );
	if( temp ){
		param.apply( temp );
	}
}
final public function tdataCall( $key:String, $returnKey:String, $method:String, ...$methodParam ):void{
	orderAdd( null, function():void{
		var temp:*, param:Array;
		temp = tdata( $key );
		if( temp ){
			if( $returnKey ){
				if( SELF._trees[$returnKey] ){
					SELF._trees[$returnKey].data = temp[$method].apply( null, $methodParam );
				}else{
					addTree( '@DATA', $returnKey, 'data', temp[$method].apply( null, $methodParam ), null );
				}
			}else{
				temp[$method].apply( null, $methodParam );
			}
		}
	} );
}